package com.molyfun.parents.activity.common.attachment;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.ImageViewerActivity;
import com.molyfun.parents.activity.common.VideoPlayerActivity;
import com.molyfun.parents.rest.bean.MediaobjectlistBean;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static com.molyfun.parents.utils.Const.VIDEO_COVER_SUFFIX;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/28/16.
 * 展示附件使用
 */
public class CartViewer extends RecyclerView implements View.OnClickListener {

    private static final int MAX_ATTACHMENT_COUNT = 9;

    private List<? extends MediaobjectlistBean> mAccachmentList;

    private Context context;
    GridLayoutManager mGridLayoutManager;
    private AttachmentViewAdapter mAttachmentAdapter;

    public CartViewer(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public CartViewer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CartViewer(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        mGridLayoutManager = new GridLayoutManager(context, 2);
        setLayoutManager(mGridLayoutManager);
        setHasFixedSize(true);

        mAccachmentList = new ArrayList<>(MAX_ATTACHMENT_COUNT);
        mAttachmentAdapter = new AttachmentViewAdapter();
        setAdapter(mAttachmentAdapter);
    }

    public void initData(List<? extends MediaobjectlistBean> attachmentViewList) {
        mAccachmentList = attachmentViewList;
        mAttachmentAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }


    public class AttachmentViewAdapter extends Adapter<ViewHolder> {

        public AttachmentViewAdapter() {
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new AttachmentViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.layout_cart_cell, parent, false));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {

//            final TopicBean aidsEntity=mList.get(position);
            AttachmentViewHolder attachmentViewHolder = (AttachmentViewHolder) holder;

            final MediaobjectlistBean mediaAttachment = mAccachmentList.get(position);

            switch (mediaAttachment.getType()) {
                //图片
                case 0:
                case 1:
                    attachmentViewHolder.mPlayBtn.setVisibility(View.GONE);
                    if (!TextUtils.isEmpty(mediaAttachment.getUrl())) {
                        Picasso.with(getContext())
                                .load(mediaAttachment.getUrl())
                                .placeholder(R.mipmap.ic_video_holder)
                                .into(attachmentViewHolder.mAttachmentImg);
                    }
                    break;
                //视频
                case 2:
                    attachmentViewHolder.mPlayBtn.setVisibility(View.VISIBLE);

                    if(!TextUtils.isEmpty(mediaAttachment.getUrl())){
                        Picasso.with(getContext())
                                .load(mediaAttachment.getUrl() + VIDEO_COVER_SUFFIX)
                                .placeholder(R.mipmap.ic_video_holder)
                                .into(attachmentViewHolder.mAttachmentImg);
                    } else {
                        attachmentViewHolder.mAttachmentImg.setImageResource(R.mipmap.ic_video_holder);
                    }
                    break;
            }

            attachmentViewHolder.mCartName.setText(mediaAttachment.getName());
            attachmentViewHolder.mAttachmentImg.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (mediaAttachment.getType()) {
                        //图片
                        case 0:
                        case 1:
                            //TODO 浏览大图
                            ImageViewerActivity.startActivity(context, mediaAttachment.getUrl());
                            break;
                        //视频
                        case 2:
                            VideoPlayerActivity.startActivity(context, null, mediaAttachment.getUrl());
                            break;
                    }

                }
            });

        }

        @Override
        public int getItemCount() {
            if (mAccachmentList == null) {
                return 0;
            }
            return mAccachmentList.size();
        }

        public class AttachmentViewHolder extends ViewHolder {

            @InjectView(R.id.attachment_img)
            ImageView mAttachmentImg;
            @InjectView(R.id.cart_name)
            TextView mCartName;
            @InjectView(R.id.video_play_btn)
            ImageView mPlayBtn;

            public AttachmentViewHolder(View view) {
                super(view);
                ButterKnife.inject(this, view);
            }
        }
    }

}